/*
 * Cluster class
 * the color param is color when we draw in 2dGraphics
 * 
 * */

package kmeansclustering;

import java.util.ArrayList;
import java.util.Iterator;

public class Cluster {
	public int idCluster;
	public Point Centroid;
	public ArrayList<Point> points;
	public int color;
	
	public Cluster(int id, Point Centroid, ArrayList<Point> points){
		this.idCluster = id;
		this.Centroid = Centroid;
		this.points = points;
	}
	
	public void addPoints(Point point){
		this.points.add(point);
	}
	
	public int sumOfPoint(){
		return this.points.size();
	}
	
	// calculate then reset centroid of cluster
	public void resetCentroid(){
		float[] propOfCentroid = new float[24];
		Iterator<Point> iterator = this.points.iterator(); // all points in this cluster
		for (int i = 0; i < this.points.size(); i++) {
			for (int j = 0; j < 24; j++) {
				propOfCentroid[j] += this.points.get(i).property[j];
			}
		}
		
		for (int i = 0; i < propOfCentroid.length; i++) {
			propOfCentroid[i] = propOfCentroid[i]/24;
		}
		
		this.Centroid = new Point(propOfCentroid, this.idCluster);
	}
	
	// calculate Euclid distance between Centroid and a point
	public float calculateEuclidDistance(Point point){
//		int[] arrayOfDesPoint = point.convertPropToArray();
		float Sum = 0.0f;
                for(int i=0;i<this.Centroid.property.length;i++) {
                    Sum = (float) (Sum + Math.pow((this.Centroid.property[i] - point.property[i]),2.0));
                }
                return (float) Math.sqrt(Sum);
	}
	
	// calculate sum of Error in this cluster
	public float calculateError(){
		float sumError = 0.0f;
                // Phai (nen) la khoang cach binh phuong moi dung cthuc.
		Iterator<Point> iterator = this.points.iterator();
		while (iterator.hasNext()){
			sumError += this.calculateEuclidDistance(iterator.next());
		}
		return sumError;
	}
	
	public ArrayList<Integer> getIdAllPoint(){
		ArrayList<Integer> arrId = new ArrayList<>();
		Iterator<Point> iterator = this.points.iterator();
		while(iterator.hasNext()){
			arrId.add(iterator.next().id);
		}
		return arrId;
	}
	
	
	public void displayPoint(){
		Iterator<Point> iterator = this.points.iterator();
//		System.err.println("size of cluster :" + this.points.size());
		while(iterator.hasNext()){
			System.out.println("point : " + iterator.next().id);
		}
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
